﻿using System.Collections.Generic;
using System.Diagnostics;
using InterfaceXAML.Enum;
using InterfaceXAML.ModelBinding;
using InterfaceXAML.ExaPlanningService;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Navigation;

// The Items Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234233

namespace InterfaceXAML.CalendarPages
{
    /// <summary>
    /// A page that displays a collection of item previews.  In the Split Application this page
    /// is used to display and select one of the available groups.
    /// </summary>
    public sealed partial class MainCalendarPage : InterfaceXAML.Common.LayoutAwarePage
    {

        public MainCalendarPage()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property provides the collection of items to be displayed.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            //this.DefaultViewModel["Items"] = e.Parameter;
            InitPage();
        }

        private async void InitPage()
        {
            InitializeComponent();
            var model = new WeekCalendarPageModel { CurrentYear = Utilities.Utilities.CurrentYear, MonthCalendar = new List<MonthCalendarModel>() };

            var reponse = await App.ExaPlanningServiceClient.GetCalendarYearAsync(Utilities.Utilities.CurrentYear);
            
            for (var i = 1; i < 13; i++)
            {
                model.MonthCalendar.Add(new MonthCalendarModel { Month = ((MonthEnum)i).ToString() });
                                
                model.MonthCalendar[i - 1].Projects = reponse.CalendarYearProjectSet[i - 1].ProjectSet;
            }

            DataContext = model;
            UpdateLayout();
        }


        private void BackButtonClick(object sender, RoutedEventArgs e)
        {
            if (Frame.CanGoBack)
            {
                Frame.GoBack();
            }
        }

        private void PrevYearButtonClick(object sender, RoutedEventArgs e)
        {
            Utilities.Utilities.CurrentYear--;
            InitPage();
        }

        private void NextYearButtonClick(object sender, RoutedEventArgs e)
        {
            Utilities.Utilities.CurrentYear++;
            InitPage();
        }


        private void SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count <= 0 || e.AddedItems[0] == null) return;

            var item = e.AddedItems[0] as MonthCalendarModel;
            MonthEnum month;

            if (!MonthEnum.TryParse(item.Month, true, out month)) return;

            Utilities.Utilities.CurrentMonth = (int)month;
            Frame.Navigate(typeof (WeekCalendarPage));
        }

    }
}
